package com.greenhouse.controller;

import com.greenhouse.config.Request;
import com.greenhouse.model.Equipment;
import com.greenhouse.model.User;
import com.greenhouse.request.EquipmentRequest;
import com.greenhouse.service.EquipmentService;
import com.greenhouse.utily.SessionUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;
    private String getEquipmentTableName(Integer id) {
        return  "e_" + id;
    }
    /**
     * 查询大棚设备表
     */
    @RequestMapping("/selectAll")
    public Request<List<EquipmentRequest>> selectEquipment(HttpServletRequest request){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error();
        }
        Integer id = user.getId();
        return equipmentService.selectEquipment(getEquipmentTableName(id),id);
    }
    /**
     * 添加大棚设备
     */
    @RequestMapping("/add")
    public Request<Equipment> addEquipment(HttpServletRequest request, Equipment equipment,Integer greenhouseId){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error();
        }
        return equipmentService.addEquipment(getEquipmentTableName(user.getId()),equipment,greenhouseId);
    }
    /**
     * 编辑大棚设备信息
     */
    @RequestMapping("/update")
    public Request<String> updateEquipment(HttpServletRequest request, String name,Integer id){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error();
        }
        return equipmentService.updateEquipment(getEquipmentTableName(user.getId()),id,name);
    }
    /**
     * 删除设备
     */
    @RequestMapping("/delete")
    public Request<String> deleteEquipment(HttpServletRequest request,Integer id){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error();
        }
        return equipmentService.deleteEquipment(getEquipmentTableName(user.getId()),id);
    }
    /**
     * 修改设备状态
     */
    @RequestMapping("/updateStatus")
    public Request<String> updateEquipmentStatus(HttpServletRequest request, Integer id, Integer status){
        User user = SessionUtils.getSessionUser(request);
        if (user == null){
            return Request.error();
        }
        return equipmentService.updateEquipmentStatus(getEquipmentTableName(user.getId()),id,status);
    }

}
